
#include <iostream>
#include <string.h>
#include <assert.h>
#include <algorithm>

using namespace std;

namespace my_vector
{
    template<class T>
    class vector 
    {
    public:
        typedef T* iterator;
        typedef const T* const_iterator;
        
        iterator begin()
        {
            return _start;
        }
        iterator end()
        {
            return _finish;
        }
        const_iterator begin() const 
        {
            return _start;
        }
        const_iterator end() const 
        {
            return _finish;
        }

        vector()
            :_start(nullptr)
             ,_finish(nullptr)
             ,_end_of_storage(nullptr)
        {}
        
        template<class InputIterator>
        vector(InputIterator first, InputIterator last)
            :_start(nullptr)
             ,_finish(nullptr)
             ,_end_of_storage(nullptr)
        {
            while (first != last)
            {
                push_back(*first);
                ++ first;
            }
        }

        vector(const vector<T>& v)
            :_start(nullptr)
             ,_finish(nullptr)
             ,_end_of_storage(nullptr)
        {
            vector tmp(v.begin(), v.end());
            swap(tmp);
        }

        void swap(vector<T>& v)
        {
            ::swap(_start, v._start);
            ::swap(_finish, v._finish);
            ::swap(_end_of_storage, v._end_of_storage);
        }

        vector<T>& operator=(vector<T> v) 
        {   
            swap(v);
            return *this;
        }

        vector(size_t n, const T& x = T())
            :_start(nullptr)
             ,_finish(nullptr)
             ,_end_of_storage(nullptr)
        {
            reserve(n);
            for (size_t i = 0; i < n; i ++ )
            {
                push_back(x);
            }
        }
        vector(int n, const T& x = T())
            :_start(nullptr)
             ,_finish(nullptr)
             ,_end_of_storage(nullptr)
        {
            reserve(n);
            for (int i = 0; i < n; i ++ )
            {
                push_back(x);
            }
        }

        ~vector()
        {
            delete[] _start;
            _start = _finish = _end_of_storage = nullptr;
        }

        size_t capacity() const 
        {
            return _end_of_storage - _start;
        }

        size_t size() const 
        {
            return _finish - _start;
        }

        void reserve(size_t n)
        {
            if (n > capacity())
            {
                T* tmp = new T[n];
                size_t oldSize =size();
                
                if (_start)
                {
                    for (size_t i = 0; i < oldSize; i ++ )
                    {
                        tmp[i] = _start[i];
                    }

                    delete[] _start;
                }

                _start = tmp;
                _finish = _start + oldSize;
                _end_of_storage = _start + n;
            }
        }

        void pop_back()
        {
            assert(!empty());

            -- _finish;
        }

        void resize(size_t n, const T& x = T())
        {
            if (n > capacity())
            {
                reserve(n);
            }

            if (size() > n)
            {
                _finish = _start + n;
            }
            else 
            {
                // 初始化填值
                while (_finish < _start + n)
                {
                    *_finish = x;
                    ++ _finish;
                }
            }
        }

        void push_back(const T& x)
        {
            if (_finish == _end_of_storage)
            {
                reserve(capacity() == 0 ? 4 : capacity() * 2);
            }

            *_finish = x;
            ++ _finish;
        }

        iterator insert(iterator pos, const T& x)
        {
            assert(pos <= _finish && pos >= _start);

            if (_finish == _end_of_storage)
            {
                size_t sz = pos - _start;
                reserve(capacity() == 0 ? 4 : capacity() * 2);
                pos = _start + sz;
            }

            iterator end = _finish;
            while (end > pos)
            {
                *end = *(end - 1);
                end -- ;
            }
            *pos = x;

            ++ _finish;

            return pos;
        }

        iterator erase(iterator pos)
        {
            assert(pos < _finish && pos >= _start);

            iterator begin = pos;
            while (begin < _finish - 1)
            {
                *begin = *(begin + 1);
                begin ++ ;
            }

            -- _finish;

            return pos;
        }

        const T& operator[](size_t pos) const 
        {
            assert(pos < size());

            return _start[pos];
        }
        T& operator[](size_t pos)
        {
            assert(pos < size());

            return _start[pos];
        }

        T& back()
        {
            assert(!empty());
                
            return *(_finish - 1);
        }

        T& front()
        {
            assert(!empty());

            return *_start;
        }

        bool empty() const 
        {
            return _start == _finish;
        }

    private:
        iterator _start;
        iterator _finish;
        iterator _end_of_storage;
    };

    void test1()
    {
        vector<int> v;
        v.push_back(1);
        v.push_back(2);
        v.push_back(3);
        v.push_back(4);
        v.push_back(5);
        v.push_back(6);
        v.push_back(7);

        for (size_t i = 0; i < v.size(); i ++ )
        {
            cout << v[i] << ' ';
        }
        cout << endl;
        
        vector<int>::iterator it = v.begin();
        while (it != v.end())
        {
            cout << *it << ' ';
            ++ it;
        }
        cout << endl;
    }

    void test2()
    {
        vector<int> v1(10, 1);
        for (size_t i = 0; i < v1.size(); i ++ )
        {
            cout << v1[i] << ' ';
        }
        cout << endl;

        vector<int> v2;
        v2.push_back(100);
        v2.push_back(199);
        v2.push_back(200);
        v2.push_back(198);
        v2.push_back(999);
        v2.push_back(555);

        for (size_t i = 0; i < v2.size(); i ++ )
        {
            cout << v2[i] << ' ';
        }
        cout << endl;

        vector<int> v3 = v2;
        for (size_t i = 0; i < v3.size(); i ++ )
        {
            cout << v3[i] << ' ';
        }
        cout << endl;

        v2 = v1;
        for (size_t i = 0; i < v1.size(); i ++ )
        {
            cout << v1[i] << ' ';
        }
        cout << endl;
    }

    void test3()
    {
        vector<int> v;
        v.reserve(20);
        cout << v.capacity() << endl;
        v.push_back(1);
        v.push_back(2);
        v.push_back(3);
        v.push_back(4);
        v.push_back(5);
        v.push_back(6);
        v.push_back(7);
        v.push_back(8);
        cout << v.capacity() << endl;
        for (size_t i = 0; i < v.size(); i ++ )
        {
            cout << v[i] << endl;
        }
        cout << endl;

        v.insert(find(v.begin(), v.end(), 6), 99999);
        v.erase(find(v.begin(), v.end(), 8));

        for (size_t i = 0; i < v.size(); i ++ )
        {
            cout << v[i] << ' ';
        }
        cout << endl;
    }
}
